﻿using FarPoint.Web.Spread;
using FarPoint.Web.Spread.Chart;
using FarPoint.Web.Spread.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.Sparkline
{
  public partial class Overview : SpreadDemoPage
  {
    private BulletChartSparklineGroup _bullGroup = null;
    private Dictionary<string, SparklineType> SparklineTypes = new Dictionary<string, SparklineType>()
    {
      {"Line", SparklineType.Line},
      {"Column", SparklineType.Column},
      {"WinLoss", SparklineType.Winloss}
    };

    internal SheetView ActiveSheet
    {
      get { return FpSpread1.ActiveSheetView; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack) return;

      PrepairLayout();

      PrepairSparklines();
      PrepairControls();
      this.FpSpread1.UseClipboard = false;
    }

    private void PrepairLayout()
    {
      //Main master = Master as Main;
      //if (master != null)
      //{
      //  master.FeatureName = "Sparklines feature demo";
      //  master.SpecUrl = "#";
      //}

      //Open sample data
      FpSpread1.Open(Server.MapPath("data.xml"));

      FpSpread1.ClientAutoCalculation = true;


      #region Ajust Spread Layout for a better view

      ActiveSheet.Columns[0].Width = 60;
      ActiveSheet.Columns[5].Width = 70;
      ActiveSheet.Columns[6].Width = 70;
      ActiveSheet.Columns[7].Width = 70;
      ActiveSheet.Columns[8].Width = 70;
      ActiveSheet.Columns[11].Width = 160;

      #endregion
    }

    private void PrepairSparklines()
    {
      SheetView sv = FpSpread1.ActiveSheetView;

      //First demostration

      ExcelSparklineSetting setting1 = new ExcelSparklineSetting();
      setting1.MarkersColor = Color.Brown;
      setting1.ShowHigh = true;
      setting1.ShowLow = true;
      setting1.ShowMarkers = true;
      setting1.HighMarkerColor = Color.Orange;
      setting1.LowMarkerColor = Color.Red;
      setting1.LastMarkerColor = Color.SpringGreen;
      setting1.NegativeColor = Color.Red;

      //1.Create a Column Sparkline
      ExcelSparkline columnSparkline = new ExcelSparkline(4, 9, "Sheet1!E5:I5");
      //2. Create a Sparkline Group with an ExcelSparklineSetting and Sparkline type
      ExcelSparklineGroup groupColumn = new ExcelSparklineGroup(setting1, SparklineType.Column);
      //3. Add Sparkline to group
      groupColumn.Add(columnSparkline);
      //4. Register Sparkline group with SheetView Sparkline container object
      sv.SparklineContainer.Add(groupColumn);

      //1.Create a Line Sparkline
      ExcelSparkline lineSparkline = new ExcelSparkline(5, 9, "Sheet1!E5:I5");
      //2. Create a Sparkline Group
      ExcelSparklineGroup groupLine = new ExcelSparklineGroup(setting1, SparklineType.Line);
      //3. Add Sparkline to group
      groupLine.Add(lineSparkline);
      //4. Register Sparkline group with SheetView Sparkline container object
      sv.SparklineContainer.Add(groupLine);

      //Second
      ExcelSparklineSetting winlossSetting = new ExcelSparklineSetting();
      winlossSetting.NegativeColor = Color.Red;
      winlossSetting.ShowNegative = true;
      ExcelSparkline winlossSparkline = new ExcelSparkline(8, 9, "Sheet1!E9:I9");
      ExcelSparklineGroup groupWinloss = new ExcelSparklineGroup(winlossSetting, SparklineType.Column);
      groupWinloss.Add(winlossSparkline);
      sv.SparklineContainer.Add(groupWinloss);


      //Third
      ExcelSparklineSetting setting2 = new ExcelSparklineSetting();
      setting2.MarkersColor = Color.Brown;
      setting2.ShowHigh = true;
      setting2.ShowLow = true;
      setting2.ShowMarkers = true;
      setting2.ShowFirst = true;
      setting2.ShowLast = true;
      setting2.HighMarkerColor = Color.BlueViolet;
      setting2.LowMarkerColor = Color.Blue;
      setting2.LastMarkerColor = Color.SpringGreen;
      setting2.NegativeColor = Color.Red;

      ExcelSparkline lineSparkline2 = new ExcelSparkline(15, 9, "Sheet1!E16:I16");
      ExcelSparklineGroup groupLine2 = new ExcelSparklineGroup(setting2, SparklineType.Line);
      groupLine2.Add(lineSparkline2);
      sv.SparklineContainer.Add(groupLine2);

      //Fourth
      ExcelSparklineSetting groupSetting = new ExcelSparklineSetting();
      groupSetting.ShowHigh = true;
      groupSetting.ShowLow = true;
      groupSetting.HighMarkerColor = Color.Orange;
      groupSetting.LowMarkerColor = Color.Red;

      //Create storage group
      ExcelSparklineGroup groupColumn2 = new ExcelSparklineGroup(groupSetting, SparklineType.Column);

      //Push Sparklines
      for (int row = 23; row <= 26; row++)
      {
        ExcelSparkline cs = new ExcelSparkline(row, 9, sv, new CellRange(row, 5, 1, 4));
        groupColumn2.Add(cs);
      }
      //Add to sheetview sparkline container
      sv.SparklineContainer.Add(groupColumn2);

      //Add some Bullet Sparkline that demostrating the customizable features of Sparkline
      for (int i = 32; i < 39; i++)
      {
        BindSparklineInRow(i);
      }

      //Add some note tooltip
      sv.Cells[4, 9].Note = "Columm sparkline referencing E5:I5";
      sv.Cells[5, 9].Note = "Line sparkline referencing E5:I5";
      sv.Cells[8, 9].Note = "Winloss sparkline referencing E9:I9";
      sv.Cells[15, 9].Note = "Line sparkline referencing E16:I16";
      sv.Cells[23, 9, 26, 9].Note = "Group of column Sparklines referencing F24:I27";
    }

    protected override void OnLoadComplete(EventArgs e)
    {
      base.OnLoadComplete(e);

      AttachSparklineCellTypes();
    }

    private void AttachSparklineCellTypes()
    {
      if (ActiveSheet.SparklineContainer.Count > 0)
      {
        foreach (ISparklineGroup group in ActiveSheet.SparklineContainer)
        {
          ExcelSparklineGroup g = group as ExcelSparklineGroup;
          if (g == null) continue;
          foreach (ExcelSparkline sp in group)
          {
            ActiveSheet.Cells[sp.Row, sp.Column].CellType = new SparklineEnhanceCellType(sp);
          }
        }
      }
    }

    private void PrepairControls()
    {
      DropDownList[] colorList = new DropDownList[] { ddlFirstColor, ddlHighColor, ddlLastColor, ddlLowColor, ddlMarkerColor, ddlNegativeColor };
      foreach (DropDownList ddl in colorList)
      {
        Utils.FillKnowColors(ddl);
      }
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
      try
      {
        string location = txtLocation.Text;
        string type = ddlTypes.SelectedValue;
        if (!string.IsNullOrEmpty(location))
        {
          if (ActiveSheet.SelectionModel.Count > 0)
          {
            CellRange dataRange = ActiveSheet.SelectionModel[0];

            CellRange locationRange = CellRange.Parse(location);

            ExcelSparklineGroup createdGroup = ActiveSheet.AddSparkline(ConvertToSheetCellRange(dataRange), locationRange, SparklineTypes[type], new ExcelSparklineSetting());

            foreach (ExcelSparkline createdSparkline in createdGroup)
            {
              ActiveSheet.Cells[createdSparkline.Row, createdSparkline.Column].CellType = new SparklineEnhanceCellType(createdSparkline);
            }
          }
        }

        lblServerMessage.Text = "";
      }
      catch (Exception ex)
      {
        lblServerMessage.Text = "Invalid data/location range!";
      }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
      string selected = txtSelectedSparkline.Text;
      if (!string.IsNullOrEmpty(selected))
      {
        string[] indexes = selected.Split(',');
        int row = Convert.ToInt32(indexes[0]);
        int col = Convert.ToInt32(indexes[1]);

        FarPoint.Web.Spread.Sparkline sp = ActiveSheet.GetSparkline(row, col);
        if (sp != null)
        {
          ExcelSparklineSetting setting = sp.Setting as ExcelSparklineSetting;
          setting.ShowFirst = chkShowFirstMarker.Checked;
          setting.ShowHigh = chkShowHighMarker.Checked;
          setting.ShowLast = chkShowLastMarker.Checked;
          setting.ShowLow = chkShowLowMarker.Checked;
          setting.ShowMarkers = chkShowMarker.Checked;
          setting.ShowNegative = chkShowNegativeMarker.Checked;
          setting.DisplayXAxis = chkShowAxis.Checked;
          setting.RightToLeft = chkPlotDataRightToLeft.Checked;

          setting.FirstMarkerColor = Color.FromName(ddlFirstColor.SelectedValue);
          setting.LastMarkerColor = Color.FromName(ddlLastColor.SelectedValue);
          setting.MarkersColor = Color.FromName(ddlMarkerColor.SelectedValue);
          setting.NegativeColor = Color.FromName(ddlNegativeColor.SelectedValue);
          setting.HighMarkerColor = Color.FromName(ddlHighColor.SelectedValue);
          setting.LowMarkerColor = Color.FromName(ddlLowColor.SelectedValue);
        }
      }
    }

    private SheetCellRange ConvertToSheetCellRange(CellRange range)
    {
      return new SheetCellRange(ActiveSheet, range.Row, range.Column, range.RowCount, range.ColumnCount);
    }

    private void BindSparklineInRow(int row)
    {
      BulletChartSparklineSetting bulletSetting = new BulletChartSparklineSetting(Color.Gray, false, 10.0);
      BulletChartSparklineGroup bulletGroup = new BulletChartSparklineGroup();
      bulletGroup.Setting = bulletSetting;
      SheetCellRange a1 = new SheetCellRange(this.FpSpread1.Sheets[0], row, 5, 1, 1);
      SheetCellRange a2 = new SheetCellRange(this.FpSpread1.Sheets[0], row, 6, 1, 1);
      SheetCellRange a3 = new SheetCellRange(this.FpSpread1.Sheets[0], row, 7, 1, 1);
      SheetCellRange a4 = new SheetCellRange(this.FpSpread1.Sheets[0], row, 8, 1, 1);
      SheetCellRange a5 = new SheetCellRange(this.FpSpread1.Sheets[0], row, 9, 1, 1);
      SheetCellRange a6 = new SheetCellRange(this.FpSpread1.Sheets[0], row, 10, 1, 1);
      BulletChartSparkline bulletSparkline = new BulletChartSparkline(ActiveSheet, row, 11, a1, a2, a3, a4, a5, a6);
      if (_bullGroup == null)
      {
        _bullGroup = new BulletChartSparklineGroup();
        _bullGroup.Setting = bulletSetting;
        _bullGroup.Add(bulletSparkline);
        ActiveSheet.SparklineContainer.Add(_bullGroup);
      }
      else
      {
        _bullGroup.Add(bulletSparkline);
      }
    }
  }
}